﻿using System;
using System.IO;
using System.Net;
using System.Data;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Markup;
using FTP = FTPSClient;

namespace Vision.Core.ActivityComponents.IO.Ftp
{
    /// <summary>
    /// 
    /// </summary>
    [Designer(typeof(FtpDirectoryFileListDesigner))]
    [ContentProperty("Body")]
    public class FtpDirectoryFileList : NativeActivity
    {                
        /// <summary>
        /// 
        /// </summary>
        internal FTP.FTPSClient FtpClient { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue("")]
        [Category("Input")]
        public InArgument<string> Where { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DefaultValue(null)]
        public ActivityAction<FTP.DirectoryListItem> Body { get; set; }

        /// <summary>
        /// 
        /// </summary>        
        public bool IncludeSubDirectories { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public FtpDirectoryFileList()
        {
            this.IncludeSubDirectories = true;
        }

        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            RuntimeArgument whereArgument = new RuntimeArgument("Where", typeof(string), ArgumentDirection.In, false);
            metadata.Bind(this.Where, whereArgument);
            metadata.AddArgument(whereArgument);

            metadata.AddDelegate(this.Body);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            this.FtpClient = (FTP.FTPSClient)context.Properties.Find("5AED2CE1-FB3C-45B3-9CB7-3DC8CD5E4C56");            
            if (this.FtpClient != null){                
                IList<FTP.DirectoryListItem> dirList = this.FtpClient.GetDirectoryList();

                if (!string.IsNullOrEmpty(this.Where.Get(context)))
                {
                    try
                    {
                        dirList = System.Linq.Dynamic.DynamicQueryable.Where(dirList.AsQueryable(), this.Where.Get(context), null).ToList();
                    }
                    catch (Exception ex)
                    {
                        // do nothing
                    }
                }

                foreach (FTP.DirectoryListItem item in dirList){
                    if (this.IncludeSubDirectories == true || item.IsDirectory == false){                                                
                        context.ScheduleAction(this.Body, item); 
                    }                                        
                }                                
            }
        }                       
    }

    /// <summary>
    /// 
    /// </summary>
    public class FtpDirectoryFileListWithBodyFactory : IActivityTemplateFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public Activity Create(DependencyObject target){
            return new FtpDirectoryFileList(){
                Body = new ActivityAction<FTP.DirectoryListItem>(){
                    Argument = new DelegateInArgument<FTP.DirectoryListItem>(){
                        Name = "item"
                    }
                }
            };
        }
    }    
}
